#!/bin/bash

#Configure
Hostname="example.dynv6.net"
IPVERSION="6" #IPV4=4, IPV6=6
UpdateURL="http://dynv6.com/api/update?hostname=$Hostname&ipv6=auto&token=YOUR_TOKEN"

RecordType=$([ "$IPVERSION" == 4 ] && echo "A" || echo "AAAA")

while true; do
 SavedIP=$(dig -$IPVERSION +short $RecordType $Hostname) #check IP which is stored on dynamic DNS
 ActualIP=$(dig -$IPVERSION +short $RecordType myip.opendns.com @resolver1.opendns.com) #get actual IP

 if [ "$SavedIP" != "$ActualIP" ] ; then
	wget -qO- $UpdateURL &> /dev/null
	if [[ $? -ge 0 ]]; then
	 SavedIP=$ActualIP
	fi
 fi

 sleep 5s #run every 5 seconds
done